home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / jpeg_image.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  5.4 KB  |  203 lines

  1. //
  2. // "$Id: jpeg_image.cxx,v 1.5 1999/01/07 19:17:56 mike Exp $"
  3. //
  4. // fl_draw_image test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Be sure to try every visual with the -v switch and try -m (monochrome)
  7. // on each of them.
  8. //
  9. // This program requires either the libjpeg.a library or an internal DD
  10. // library to read images (this is chosen by the presence of the "DD"
  11. // #define).
  12. //
  13. // To get the jpeg library:
  14. //
  15. // The "official" archive site for this software is ftp.uu.net (Internet
  16. // address 192.48.96.9).  The most recent released version can always be
  17. // found there in directory graphics/jpeg.  This particular version will
  18. // be archived as graphics/jpeg/jpegsrc.v6a.tar.gz.
  19. //
  20. // The makefile assummes you decompressed and build these in a directory
  21. // called "jpeg-6a" in the same location as the "FL" directory.
  22. //
  23. // Copyright 1998-1999 by Bill Spitzak and others.
  24. //
  25. // This library is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Library General Public
  27. // License as published by the Free Software Foundation; either
  28. // version 2 of the License, or (at your option) any later version.
  29. //
  30. // This library is distributed in the hope that it will be useful,
  31. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  33. // Library General Public License for more details.
  34. //
  35. // You should have received a copy of the GNU Library General Public
  36. // License along with this library; if not, write to the Free Software
  37. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  38. // USA.
  39. //
  40. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  41. //
  42.  
  43. #include <FL/Fl.H>
  44. #include <FL/fl_draw.H>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47.  
  48. void readtheimage(const char *name); // below
  49. int width;
  50. int height;
  51. int depth;
  52. int linedelta;
  53. uchar *ibuffer;
  54.  
  55. ////////////////////////////////////////////////////////////////
  56.  
  57. #include <FL/Fl_Window.H>
  58. int mono;
  59.  
  60. class image_window : public Fl_Window {
  61.   void draw();
  62. public:
  63.   image_window(int w,int h) : Fl_Window(w,h) {box(FL_NO_BOX);}
  64. };
  65.  
  66. void image_window::draw() {
  67.   if (mono)
  68.     fl_draw_image_mono(ibuffer+1,0,0,width,height,depth,linedelta);
  69.   else
  70.     fl_draw_image(ibuffer,0,0,width,height,depth,linedelta);
  71. }
  72.  
  73. ////////////////////////////////////////////////////////////////
  74.  
  75. #include <FL/x.H>
  76. #include "list_visuals.cxx"
  77.  
  78. ////////////////////////////////////////////////////////////////
  79.  
  80. int visid = -1;
  81. int arg(int argc, char **argv, int &i) {
  82.   if (argv[i][1] == 'm') {mono = 1; i++; return 1;}
  83.  
  84.   if (argv[i][1] == 'v') {
  85.     if (i+1 >= argc) return 0;
  86.     visid = atoi(argv[i+1]);
  87.     i += 2;
  88.     return 2;
  89.   }
  90.  
  91.   return 0;
  92. }
  93.  
  94. int main(int argc, char ** argv) {
  95.  
  96.   int i = 1;
  97.   if (!Fl::args(argc,argv,i,arg) || i != argc-1) {
  98.     fprintf(stderr,"usage: %s <switches> image_file\n"
  99. " -v # : use visual\n"
  100. " -m : monochrome\n"
  101. "%s\n",
  102.         argv[0],Fl::help);
  103.     exit(1);
  104.   }
  105.  
  106.   readtheimage(argv[i]);
  107.   image_window *window = new image_window(width,height);
  108.  
  109.   if (visid>=0) {
  110.     fl_open_display();
  111.     XVisualInfo templt; int num;
  112.     templt.visualid = visid;
  113.     fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
  114.     if (!fl_visual) {
  115.       fprintf(stderr, "No visual with id %d, use one of:\n",visid);
  116.       list_visuals();
  117.       exit(1);
  118.     }
  119.     fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
  120.                 fl_visual->visual, AllocNone);
  121.     fl_xpixel(FL_BLACK); // make sure black is allocated
  122.   }
  123.  
  124.   window->show(argc,argv);
  125.   return Fl::run();
  126. }
  127.  
  128. ////////////////////////////////////////////////////////////////
  129. #ifndef DD_LIBRARY
  130. // Read using jpeg library:
  131.  
  132. extern "C" {
  133. #include "jpeglib.h"
  134. }
  135.  
  136. void readtheimage(const char *name) {
  137.   struct jpeg_decompress_struct cinfo;
  138.   struct jpeg_error_mgr jerr;
  139.   FILE * infile = fopen(name, "rb");
  140.   if (!infile) {
  141.     fprintf(stderr, "can't open %s\n", name);
  142.     exit(1);
  143.   }
  144.   cinfo.err = jpeg_std_error(&jerr);
  145.   jpeg_create_decompress(&cinfo);
  146.   jpeg_stdio_src(&cinfo, infile);
  147.   jpeg_read_header(&cinfo, TRUE);
  148.   jpeg_start_decompress(&cinfo);
  149.   width = cinfo.output_width;
  150.   height = cinfo.output_height;
  151.   depth = cinfo.output_components;
  152.   ibuffer = new uchar[width*height*depth];
  153.   uchar *rp = ibuffer;
  154.   //  for (int i=0; i<height; i++) {
  155.   for (int i=height; i--; ) {
  156.     jpeg_read_scanlines(&cinfo, &rp, 1);
  157.     rp += width*depth;
  158.   }
  159.   jpeg_finish_decompress(&cinfo);
  160.   jpeg_destroy_decompress(&cinfo);
  161.   fclose(infile);
  162. }
  163.  
  164. ////////////////////////////////////////////////////////////////
  165. #else // Digital Domain in-house library
  166.  
  167. #include "DDNewImage/DDImageOp.H"
  168. #include "DDNewImage/DDImgRead.H"
  169. #include "DDNewImage/DDImgToBuffer.H"
  170.  
  171. void readtheimage(const char *name) {
  172.   DDImgRead reader(name);
  173.   width = reader.xsize();
  174.   height = reader.ysize();
  175.   depth = 4; // reader.zsize();
  176.   ibuffer = new uchar[width*height*depth];
  177.   DDImgToBuffer b(&reader,depth,ibuffer,0,0,width,height);
  178.   b.execute();
  179.   if (DDImage::haderror) {
  180.     fprintf(stderr,"%s\n",DDImage::errormsg());
  181.     exit(1);
  182.   }
  183.   // swap it around into RGBA order:
  184.   for (uchar *p = ibuffer+width*height*4-4; p >= ibuffer; p-=4) {
  185.     uchar r = p[3];
  186.     uchar g = p[2];
  187.     uchar b = p[1];
  188.     uchar a = p[0];
  189.     p[0] = r;
  190.     p[1] = g;
  191.     p[2] = b;
  192.     p[3] = a;
  193.   }
  194.   // make it bottom-to-top:
  195.   ibuffer = ibuffer + width*(height-1)*depth;
  196.   linedelta = -(width*depth);
  197. }
  198. #endif
  199.  
  200. //
  201. // End of "$Id: jpeg_image.cxx,v 1.5 1999/01/07 19:17:56 mike Exp $".
  202. //
  203.